package com.platform.core.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.platform.comm.dto.DtoMapper;
import com.platform.comm.service.impl.CommonBizImpl;
import com.platform.core.entity.CdDictManageEntity;
import com.platform.core.entity.QCdDictManageEntity;
import com.platform.core.repository.CdDictManageRepository;
import com.platform.core.service.CdDictManageBiz;
import com.platform.dto.CdDictManageDto;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Description: CdDictManage-业务接口实现类
 *
 * @author Coder
 */
@Slf4j
@Service
public class CdDictManageBizImpl extends CommonBizImpl<CdDictManageRepository, CdDictManageEntity, String> implements CdDictManageBiz {

    private final QCdDictManageEntity qCdDictManage = QCdDictManageEntity.cdDictManageEntity;
    private final DtoMapper mapper;

    public CdDictManageBizImpl(CdDictManageRepository cdDictManageRepository, DtoMapper mapper) {
        super(cdDictManageRepository);
        this.mapper = mapper;
    }

    @Override
    public Page<CdDictManageDto> search(Predicate predicate, Pageable pageable){
        Page<CdDictManageEntity> page = queryByPage(predicate, pageable);
        return page.map(entity -> mapper.map(entity, CdDictManageDto.class));
    }

    @Override
    public List<CdDictManageDto> detail(Map<String,String> params) {
        String groupCode = params.get("groupCode");
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qCdDictManage.groupCode.eq(groupCode));
        List<CdDictManageEntity> cdDictManageEntities = queryAll(builder);
        return cdDictManageEntities.stream().map(item -> mapper.map(item, CdDictManageDto.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String,String>> searchList() {
        BooleanBuilder builder = new BooleanBuilder();
        List<Map<String,String>> list = Lists.newArrayList();
        List<CdDictManageEntity> cdDictManageEntities = queryAll(builder);
        if (CollectionUtil.isNotEmpty(cdDictManageEntities)){
            Map<String,String> map = Maps.newHashMap();
            cdDictManageEntities.forEach(item -> map.put(item.getGroupCode(),item.getGroupName()));
            map.forEach((k,v) ->{
                Map<String,String> map1 = Maps.newHashMap();
                map1.put("key",k);
                map1.put("value",v);
                list.add(map1);
            });
            return list;
        }
        return list;
    }

    @Override
    public Map<String, List<Map<String, String>>> commonDict() {
        BooleanBuilder builder = new BooleanBuilder();
        List<Map<String,String>> list = Lists.newArrayList();
        List<CdDictManageEntity> cdDictManageEntities = queryAll(builder);
        Map<String, List<Map<String, String>>> result = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(cdDictManageEntities)){
            Map<String,String> map = Maps.newHashMap();
            cdDictManageEntities.forEach(item -> map.put(item.getGroupCode(),item.getGroupName()));
            map.forEach((k,v) ->{
                List<Map<String, String>> ll = Lists.newArrayList();
                BooleanBuilder builder1 = new BooleanBuilder();
                builder1.and(qCdDictManage.groupCode.eq(k));
                List<CdDictManageEntity> resultList = queryAll(builder1);
                resultList.forEach(item ->{
                    Map<String,String> m = Maps.newHashMap();
                    String code = item.getCode();
                    String value = item.getValue();
                    m.put("key",code);
                    m.put("value",value);
                    ll.add(m);
                });
                result.put(k,ll);
            });
            return result;
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CdDictManageDto req) {
        CdDictManageEntity entity = mapper.map(req, CdDictManageEntity.class);
        saveEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(CdDictManageDto req) {
        CdDictManageEntity entity = fetchById(req.getId());
        mapper.map(req, entity);
        saveEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String id){
        repository.deleteById(id);
    }
}
